package com.dhcc.bpm.modules.content.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.modules.content.entity.Sdk;
import com.dhcc.bpm.modules.content.service.ISdkService;
import com.dhcc.bpm.modules.content.vo.SdkVO;
import com.dhcc.bpm.modules.system.service.ISerialNumberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Map;

/**
 * @ClassName SdkController
 * @Description  SDK管理 控制器
 * @Author lijiacheng
 * @Date 2020/2/21 14:16
 * @Version V1.0
 * @Copyright: 2020 东华软件 . All rights reserved.
 */

@RestController
@AllArgsConstructor
@RequestMapping("bpm-content/sdk")
@Api(value = "架包管理", tags = "架包管理接口")
public class SdkController extends BladeController {

	private ISdkService sdkService;

	private ISerialNumberService serialNumberService;

	/**
	 * 详情
	 */
	@ApiLog("SDK模块表详情")
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入sdk")
	public R<Sdk> detail(Sdk sdk) {
		Sdk detail = sdkService.getOne(Condition.getQueryWrapper(sdk));
		return R.data(detail);
	}

	/**
	 * 详情
	 */
	@ApiLog("SDK模块表详情")
	@GetMapping("/detailSelf")
	@ApiOperationSupport(order = 9)
	@ApiOperation(value = "详情", notes = "传入sdk")
	public R<SdkVO> detailSelf(Sdk sdk) {
		SdkVO detail = sdkService.getDetailSelfById(sdk.getId());
		return R.data(detail);
	}

	/**
	 * 分页 架包管理
	 */
	@ApiLog("SDK模块表列表")
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入sdk")
	public R<IPage<Sdk>> list(@ApiIgnore @RequestParam Map<String, Object> sdk, Query query) {
		IPage<Sdk> pages = sdkService.page(Condition.getPage(query), Condition.getQueryWrapper(sdk,Sdk.class));
		return R.data(pages);
	}

	/**
	 * 自定义分页 架包管理
	 */
	@ApiLog("SDK模块表分页")
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入sdk")
// 	@PreAuth("hasPermission('desk:sdk:list')")
	public R<IPage<SdkVO>> page(@ApiIgnore SdkVO sdk, Query query) {
		IPage<SdkVO> pages = sdkService.selectSdkPage(Condition.getPage(query), sdk);
		return R.data(pages);
	}

	/**
	 * 新增 架包管理
	 */
	@ApiLog("SDK模块表新增")
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入sdk")
	@Log(title = "SDK信息新增", businessType = BusinessType.INSERT)
	public R save(@Valid @RequestBody Sdk sdk) {
		return R.status(sdkService.save(sdk));
	}

	/**
	 * 修改 架包管理
	 */
	@ApiLog("SDK模块表更新")
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入sdk")
	@Log(title = "SDK信息更新", businessType = BusinessType.UPDATE)
	public R update(@Valid @RequestBody Sdk sdk) {
		return R.status(sdkService.updateById(sdk));
	}

	/**
	 * 新增或修改 架包管理
	 */
	@ApiLog("SDK模块表提交")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入sdk")
	@Log(title = "SDK信息", businessType = BusinessType.INSERT)
	public R submit(@Valid @RequestBody Sdk sdk) {
		try {
			if (sdk.getId() == null){
				// 设置SDK编号
				String code = serialNumberService.generateSerialNumberByModelCode("opc_marketing");
				sdk.setSdkCode(code);
			}
			return R.status(sdkService.saveOrUpdate(sdk));
		} catch (Exception e) {
			if (e.getMessage().contains("for key 'sdk_name_idx'") || e.getMessage().contains("ORA-00001")) {
				return R.fail(500, "该语言版本的SDK已存在");
			}
			return R.fail(500, "数据库异常");
		} finally {

		}
	}

	
	/**
	 * 删除 架包管理
	 */
	@ApiLog("SDK模块表删除")
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	@Log(title = "SDK信息删除", businessType = BusinessType.DELETE)
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(sdkService.deleteLogic(Func.toLongList(ids)));
	}

	
}
